<template>
  <div>
    <el-card>
      <el-form :inline="true" :model="searchForm">
        <el-form-item label="角色名称">
          <el-input v-model="searchForm.role_name" placeholder="请输入角色名称" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="getRoleList">查询</el-button>
          <el-button @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>

      <el-button type="primary" @click="openAddDialog" style="margin: 10px 0">新增角色</el-button>

      <el-table :data="roleList" style="width: 100%">
        <el-table-column prop="role_name" label="角色名称" />
        <el-table-column prop="role_code" label="角色编码" />
        <el-table-column prop="description" label="描述" />
        <el-table-column prop="status" label="状态" :formatter="formatStatus" />
        <el-table-column label="操作">
          <template #default="scope">
            <el-button type="primary" link size="small" @click="editRole(scope.row)">编辑</el-button>
            <el-button type="danger" link size="small" @click="deleteRole(scope.row.role_id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
          v-model:current-page="pageNum"
          v-model:page-size="pageSize"
          :total="total"
          @current-change="getRoleList"
      />
    </el-card>
  </div>
</template>

<script setup>
import { ref } from 'vue'

const searchForm = ref({ role_name: '' })
const roleList = ref([])
const pageNum = ref(1)
const pageSize = ref(10)
const total = ref(0)

function getRoleList() {
  console.log('查询角色列表', searchForm.value)
}

function resetSearch() {
  searchForm.value.role_name = ''
  getRoleList()
}

function formatStatus(row) {
  return row.status === 1 ? '启用' : '禁用'
}

function openAddDialog() {
  console.log('打开新增角色弹窗')
}

function editRole(row) {
  console.log('编辑角色', row)
}

function deleteRole(roleId) {
  console.log('删除角色', roleId)
}
</script>
